<template>
    <div class="repayMoney">
        <group :title="loanMoney|filterMoney">
            <x-input title="还款金额" type="number" :max="loanMoney" v-model="moneyAmount" :is-type="more">
                <span slot="right">元</span>
            </x-input>
        </group>
        <div @click="confirmMoney()" class="btn">
            <span>确定</span>
        </div>
    </div>
</template>

<script>
import { XInput, Group, numberComma } from "vux";
import api from "../fetch/api";
import { getStore, setStore, removeStore } from "../config/mUtils";
export default {
  components: {
    XInput,
    Group,
    numberComma
  },
  methods: {
    confirmMoney() {
      if (this.moneyAmount < 50000) {
        this.$vux.toast.text("还款金额不得小于50000");
      } else if(this.moneyAmount > parseInt(this.loanMoney)){
          this.$vux.toast.text("还款金额不得大于贷款金额");
      }else {
        this.$router.push({
          path: "/select/time",
          query: {
            type: this.$route.query.type,
            mount: this.moneyAmount,
            lonnbr: this.$route.query.lonnbr
          }
        });
      }
    }
  },
  filters: {
    filterMoney(value) {
      return `请输入大于50000元，小于${value}元的金额`;
    }
  },
  data() {
    return {
      moneyAmount: 0,
      loanMoney: 0, // 剩余的钱
      more: function(value) {
        return {
          valid: value >= 50000,
          msg: "必须大于50000"
        };
      }
    };
  },
  activated() {
    if (this.$route.query.type === "RepayAheadall") {
      api.get_loan_way({ token: getStore("token") }).then(res => {
        if (res.success) {
          res.loantype.forEach((item, index) => {
            if (item.lonnbr === this.$route.query.lonnbr) {
              this.moneyAmount = item.bal;
              this.loanMoney = parseInt(item.bal);
            }
          });
        }
      });
    } else {
      api.get_loan_way({ token: getStore("token") }).then(res => {
        if (res.success) {
          res.loantype.forEach((item, index) => {
            if (item.lonnbr === this.$route.query.lonnbr) {
              this.loanMoney = parseInt(item.bal);
            }
          });
        }
      });
      this.moneyAmount = "";
    }
    this.currentType = this.$route.query.type;
  }
};
</script>

<style lang="less" scoped>
.repayMoney {
}
</style>

